<template>
	<sw-common-frame :loading="loading">
		<template slot="button">
			<el-button size="small" type="primary" icon="el-icon-plus" @click="handleOpenDialog()">新增</el-button>
		</template>
		<el-form slot="query" size="small" inline>
			<el-form-item>
				<el-input v-model="queryData.name" clearable placeholder="公告名称" style="width: 180px"></el-input>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" @click="queryTableData()">查询</el-button>
			</el-form-item>
		</el-form>
		<template slot="table">
			<el-table size="medium" :data="tableData" height="100%">
				<el-table-column label="序号" width="48" type="index" align="center"></el-table-column>
				<el-table-column prop="title" label="文件名称" min-width="375" show-overflow-tooltip>
					<template slot-scope="{ row }">
						<router-link :to="`/platform/announcement/${row.id}`">{{ row.title }}</router-link>
					</template>
				</el-table-column>
				<el-table-column label="状态" min-width="100" :formatter="returnDataStatus"></el-table-column>
				<el-table-column prop="createName" label="创建人" min-width="100"></el-table-column>
				<el-table-column prop="createTime" label="创建时间" min-width="160"></el-table-column>
				<el-table-column label="操作" width="136">
					<template slot-scope="{ row }">
						<template v-if="row.isSave !== '1'">
							<el-button size="medium" type="text" @click="handleUpdateStatus(row, 1)">提交</el-button>
						</template>
						<template v-else-if="row.del !== '1'">
							<el-button size="medium" type="text" @click="handleUpdateStatus(row, 2)">发布</el-button>
						</template>
						<template v-else>
							<el-button size="medium" type="text" @click="handleUpdateStatus(row, 3)">下架</el-button>
						</template>
						<el-button size="medium" type="text" @click="handleOpenDialog(row.id)" :disabled="row.del === '1'">编辑</el-button>
						<el-button size="medium" type="text" @click="handleDeleteData(row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
		</template>
		<template slot="page">
			<sw-pagination :total="dataCount" :page.sync="queryData.page" :limit.sync="queryData.limit" @change="queryTableData()"></sw-pagination>
		</template>
		<form-dialog v-model="formDialog" :data-id="dataId" @refresh="queryTableData()"></form-dialog>
	</sw-common-frame>
</template>

<script>
import formDialog from './form';
import listMixins from '@/mixins/listMixins';
import { getJournalismPageList, updateJournalism, deleteJournalism, updateJournalismStatus } from '@/api/platform/tbSysJournalism';
export default {
	mixins: [listMixins],
	components: { formDialog },
	data() {
		return {
			pageName: '通知公告'
		};
	},
	computed: {
		listFunction() {
			return eval(getJournalismPageList);
		},
		deleteFunction() {
			return eval(deleteJournalism);
		},
		requestParams() {
			return { ...this.queryData, type: '100' };
		}
	},
	methods: {
		//修改状态
		handleUpdateStatus(data, type) {
			if (type === 1) {
				this.handleSubmitRequest(data.id, '1');
			} else if (type === 2) {
				this.handleStatusRequest(data.id, '1');
			} else if (type === 3) {
				this.handleStatusRequest(data.id, '2');
			}
		},

		//提交请求
		async handleSubmitRequest(id, isSave) {
			let params = { id, isSave };
			let result = await updateJournalism(params).then((res) => res.data);
			if (result.code === '0') {
				this.queryTableData();
			}
		},

		//发布/下架请求
		async handleStatusRequest(id, del) {
			let params = { id, del };
			let result = await updateJournalismStatus(params).then((res) => res.data);
			if (result.code === '0') {
				this.queryTableData();
			}
		},

		//格式化公告状态
		returnDataStatus(data) {
			let params = {};
			if (data.isSave !== '1') {
				params = { text: '暂存', type: 'info' };
			} else if (data.del !== '1') {
				params = { text: '下架', type: 'warning' };
			} else {
				params = { text: '上架', type: 'success' };
			}
			return this.$createElement('elTag', { attrs: { size: 'medium', type: params.type } }, params.text);
		}
	}
};
</script>

<style lang="scss" scoped></style>
